/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.validate.min.js";

$('.J_add-product-form').validate({
    errorElement: 'label', //default input error message container
    errorClass: 'help-inline', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        prodNam: {
            required: true
        },
        prodBrand: {
            required: true
        }
    },

    messages: {
        prodNam: {
            required: "请填写品名"
        },
        prodBrand: {
            required: "请填写品牌"
        }
    },

    invalidHandler: function (event, validator) { //display error alert on form submit
        $('.alert-error', $('.J_add-product-form')).show();
    },

    highlight: function (element) { // hightlight error inputs
        $(element)
            .closest('.control-group').addClass('error'); // set error class to the control group
    },

    success: function (label) {
        label.closest('.control-group').removeClass('error');
        label.remove();
    },

    errorPlacement: function (error, element) {
        error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
    },

    submitHandler: function (form) {
        let $form = $(form);
        let prodNam = $form.find('.J_prodNam')[0].value;
        let prodBrand = $form.find('.J_prodBrand')[0].value;
        let prodItdc = $form.find('.J_prodItdc')[0].value;
        let prodModel = $form.find('.J_prodModel')[0].value;
        let prodNum = $form.find('.J_prodNum')[0].value;
        let prodPrik = $form.find('.J_prodPrik')[0].value;
        let token = sessionStorage.getItem('supplier_token');

        let data = {
            "prodBrand": prodBrand,
            "prodItdc": prodItdc,
            "prodModel": prodModel,
            "prodNam": prodNam,
            "prodNum": prodNum,
            "prodPrik": prodPrik,
            "rcmdOnsfId": 0
        };

        $.ajax({
            url: HOST_ADDR + '/cpmp/splrSlfMng/addSelfProd',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            access_token: token,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp){
                if(resp.success){
                    alert('新增自荐产品成功');
                    location.href = '/supplier_self/product_list.html';
                }else{
                    alert('添加自荐产品失败');
                }
            },
            error:function(err){
                console.log(err);
            }
        });
    }
});

$('.J_add-user-form .J_cancel').click(function(){
    location.href = '/supplier_self/user_manage.html';
});


